package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.service.ICouponTplService;
import com.qianniu.napi.admin.service.ICzfanService;
import com.qianniu.napi.admin.service.IFenxiaoService;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充值返现|积分
 * </p>
 *
 * @author chenxuebin
 * @since 2019-03-18
 */
@Controller
public class CzfanController {

    private static final Logger logger = LoggerFactory.getLogger(CzfanController.class);

    @Autowired
    private ICzfanService czfanService;
    @Autowired
    private IFenxiaoService fenxiaoService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ICouponTplService couponTplService;

    @RequestMapping("/api/czfan/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = czfanService.selectPage(page, new EntityWrapper<Czfan>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/czfan/add")
    @ResponseBody
    public Jinfo czfanAdd(@Valid Czfan czfan,
                          BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        czfan.setCreatetime(new Date());
        czfan.setMid(ObjectUtil.getMid());
        czfan.setMpid(ObjectUtil.getMpid());
        czfan.setUid(ObjectUtil.getUid());
        czfan.insert();
        return info;
    }

    @RequestMapping("/api/czfan/modify")
    @ResponseBody
    public Jinfo czfanUpdate(HttpServletRequest req,
                             Czfan czfan) {
        Jinfo info = new Jinfo();
        czfan.setModifytime(new Date());
        czfan.updateById();
        return info;
    }

    @RequestMapping("/api/czfan/detail")
    @ResponseBody
    public Jinfo getCzfanDetailById() {
        Jinfo info = new Jinfo();

        List<Czfan> czfan = czfanService.selectList(new EntityWrapper<Czfan>()
                .eq("mpid", ObjectUtil.getMpid())
                .orderBy("level", true)
        );

        Fenxiao fenxiao = fenxiaoService.selectOne(new EntityWrapper<Fenxiao>().eq("mpid", ObjectUtil.getMpid()));
        if (fenxiao != null) {
            for (int i = 0; i < czfan.size(); i++) {
                if (i == 0) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou1().doubleValue());
                } else if (i == 1) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou2().doubleValue());
                } else if (i == 2) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou3().doubleValue());
                } else if (i == 3) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou4().doubleValue());
                } else if (i == 4) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou5().doubleValue());
                } else if (i == 5) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou6().doubleValue());
                } else if (i == 6) {
                    czfan.get(i).setMzhekou(fenxiao.getMzhekou7().doubleValue());
                }
            }
        }
        Map<String, String> gmap = new HashMap<String, String>();
        Map<String, String> qmap = new HashMap<String, String>();

        List<Goods> glist = goodsService.selectList(new EntityWrapper<Goods>()
                .setSqlSelect("id,name")
                .eq("mpid", ObjectUtil.getMpid()));

        List<CouponTpl> qlist = couponTplService.selectList(new EntityWrapper<CouponTpl>()
                .setSqlSelect("id,name")
                .eq("mpid", ObjectUtil.getMpid()));

        for (Goods item : glist) {
            gmap.put(item.getId().toString(), item.getName());
        }
        for (CouponTpl item : qlist) {
            qmap.put(item.getId().toString(), item.getName());
        }
        logger.debug("================qmap=" + qmap);

        for (Czfan item : czfan) {

            if (item.getFangoods() != null && gmap.containsKey(item.getFangoods().toString())) {
                item.setGname(gmap.get(item.getFangoods().toString()).toString());
            }

            if (item.getFanquan() != null && qmap.containsKey(item.getFanquan().toString())) {
                item.setQname(qmap.get(item.getFanquan().toString()).toString());
            }
        }

        info.setInfo(czfan);
        return info;
    }

}
